package cn.wolfcode.luowowo.website.controller;



import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.domain.TravelCommend;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.ITravelCommendService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.cache.service.IUserInfoRegistService;
import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.*;
import cn.wolfcode.luowowo.search.template.DestinationTemplate;
import cn.wolfcode.luowowo.search.template.StrategyTemplate;
import cn.wolfcode.luowowo.search.template.TravelTemplate;
import cn.wolfcode.luowowo.search.template.UserInfoTemplate;
import cn.wolfcode.luowowo.search.vo.SearchResultVO;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class IndexController {

    @Reference
    private IUserInfoRegistService userInfoRegistService;

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private ITravelService travelService;

    @Reference
    private ITravelCommendService travelCommendService;





    @Reference
    private IUserInfoTemplateService userInfoTemplateService;

    @Reference
    private IDestinationTemplateService destinationTemplateService;

    @Reference
    private IStrategyTemplateService strategyTemplateService;

    @Reference
    private ITravelTemplateService travelTemplateService;

    @Reference
    private ISearchService searchService;


    //首页
    @RequestMapping("")
    public String index(HttpServletRequest request, Model model){

        //首页游记前5轮播
        List<TravelCommend> list = travelCommendService.selectTop4();
        model.addAttribute("tcs",list);

        //sc 旅游攻略推荐
        StrategyDetail top1 = strategyDetailService.getTop1();
        model.addAttribute("sc",top1);


        return "index/index";
    }

    //游记列表
    @RequestMapping("index/travelPage")
    public Object list(Model model, @ModelAttribute("qo")TravelQuery qo){

        PageInfo query = travelService.query(qo);
        model.addAttribute("pageInfo",query);
        return "index/travelPageTpl";
    }

    //ES搜索
    @RequestMapping("q")
    public Object query(Model model, @ModelAttribute("qo")SearchQueryObject qo){

        switch (qo.getType()){
            case SearchQueryObject.SEARCH_TYPE_DEST:
                return destSearch(model, qo);
            case SearchQueryObject.SEARCH_TYPE_STRATEGY:
                return strategySearch(model,qo);
            case SearchQueryObject.SEARCH_TYPE_TRAVEL:
                return travleSearch(model,qo);
            case SearchQueryObject.SEARCH_TYPE_USER:
                return userSearch(model,qo);
                default:return allSearch(model,qo);
        }
    }
    //查询全部
    private Object allSearch(Model model, SearchQueryObject qo) {
        Page<DestinationTemplate> dests = searchService.searchWithHighlight(DestinationTemplate.INDEX_NAME, DestinationTemplate.TYPE_NAME,
                DestinationTemplate.class, qo, "name", "info");

        Page<UserInfoTemplate> users = searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME, UserInfoTemplate.TYPE_NAME,
                UserInfoTemplate.class, qo, "nickname", "destName");

        Page<TravelTemplate> travels = searchService.searchWithHighlight(TravelTemplate.INDEX_NAME, TravelTemplate.TYPE_NAME,
                TravelTemplate.class, qo, "title", "summary");

        Page<StrategyTemplate> strategys = searchService.searchWithHighlight(StrategyTemplate.INDEX_NAME, StrategyTemplate.TYPE_NAME,
                StrategyTemplate.class, qo, "title", "subTitle","summary");


        SearchResultVO data = new SearchResultVO();

        data.setUsers(users.getContent());
        data.setTravels(travels.getContent());
        data.setStrategys(strategys.getContent());
        data.setDests(dests.getContent());

        data.setTotal(dests.getTotalElements() + users.getTotalElements() +travels.getTotalElements() +  strategys.getTotalElements());

        model.addAttribute("data", data);

        return "index/searchAll";
    }

    //用户查询
    private Object userSearch(Model model, SearchQueryObject qo) {
        //参数1: 索引 参数2:索引类型  参数3:结果list泛型 参数4:qo  参数5:查询的字段集合
        Page<UserInfoTemplate> page = searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME, UserInfoTemplate.TYPE_NAME,
                UserInfoTemplate.class, qo, "nickname", "destName");
        model.addAttribute("page", page);
        return "index/searchUser";
    }

    //游记查询
    private Object travleSearch(Model model, SearchQueryObject qo) {
        //参数1: 索引 参数2:索引类型  参数3:结果list泛型 参数4:qo  参数5:查询的字段集合
        Page<TravelTemplate> page = searchService.searchWithHighlight(TravelTemplate.INDEX_NAME, TravelTemplate.TYPE_NAME,
                TravelTemplate.class, qo, "title", "summary");

        model.addAttribute("page", page);
        return "index/searchTravel";
    }

    //攻略查询
    private Object strategySearch(Model model, SearchQueryObject qo) {
        Page<StrategyTemplate> page = searchService.searchWithHighlight(StrategyTemplate.INDEX_NAME, StrategyTemplate.TYPE_NAME,
                StrategyTemplate.class, qo, "title", "subTitle","summary");

        model.addAttribute("page", page);
        return "index/searchStrategy";
    }

    //目的地查询
    private Object destSearch(Model model, SearchQueryObject qo) {
        SearchResultVO data = new SearchResultVO();
        //1:查询目的地对象
        DestinationTemplate dest = destinationTemplateService.findByName(qo.getKeyword());
        if(dest != null){
            //2:查该目的地下的所有攻略, 前5个
            long total = 0;
            List<StrategyTemplate> sts = strategyTemplateService.findByDestName(dest.getName());
            total += sts.size();
            if(sts.size() > 5){
                sts = sts.subList(0, 5);
            }
            //3:查该目的地下的所有游记, 前5个
            List<TravelTemplate> ts = travelTemplateService.findByDestName(dest.getName());
            total += ts.size();
            if(ts.size() > 5){
                ts = ts.subList(0, 5);
            }
            //4:查该目的地下的所有用户, 前5个
            List<UserInfoTemplate> us = userInfoTemplateService.findByDestName(dest.getName());
            total += us.size();
            if(us.size() > 5){
                us = us.subList(0, 5);
            }
            data.setStrategys(sts);
            data.setTravels(ts);
            data.setUsers(us);
            data.setTotal(total);

            model.addAttribute("data", data);
        }
        model.addAttribute("dest", dest);

        return "index/searchDest";
    }


}
